﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using elfam.web.Dao;
using elfam.web.Models;
using elfam.web.Paging.Prototype;

namespace elfam.web.Paging.Order
{
    public class OrderListQueryBuilder : QueryBuilder<Models.Order>
    {
        private User _user;
        DaoTemplate _daoTemplate = new DaoTemplate();
        
        public OrderListQueryBuilder(OrderListSearchCriteria criteria) 
            : base(criteria)
        {
            if (criteria.UserId.HasValue)
                _user = _daoTemplate.FindByID<User>(criteria.UserId);
        }

        protected override Dictionary<string, string> initAliases()
        {
            return new Dictionary<string, string>()
                       {
                           {"", ""}
                       };
        }

        protected override string getTotalCountHql()
        {
            string hql = "select count(o.Id) from Order o";
            if (_user != null)
                hql += " where o.User.Id = " + _user.Id;
            return hql;
        }

        protected override string getHql()
        {
            string hql =
            @"select 
            o.Id,
            o.Date,
            o.User.Name,
            sum(out.Price * out.Quantity * ()) as sum,
            sum  
            
            from Order o 
        
            join o.Lines ol 
            join ol.Outcomes out
            group by o.Id";
            if (_user != null)
                hql += " where o.User.Id = " + _user.Id;
            return hql;
        }
    }
}
